package com.charm.watercress.ui.activity

import android.content.Intent
import android.os.Bundle
import android.support.design.widget.AppBarLayout
import android.support.design.widget.CollapsingToolbarLayout
import android.support.v4.app.ActivityOptionsCompat
import android.support.v4.widget.NestedScrollView
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.Toolbar
import android.view.*
import android.widget.ImageView
import android.widget.TextView
import cn.bingoogolapple.bgabanner.BGABanner
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.charm.codes.imageLoad.XGlide
import com.charm.watercress.R
import com.charm.watercress.adapter.ActorsAdapter
import com.charm.watercress.adapter.VideosAdapter
import com.charm.watercress.entity.FilmEntity
import com.google.gson.Gson

/**
 * Created by tools on 2018/1/11.
 */
@Route(path = "/watercress/film/detail")
class FilmDetailActivity : AppCompatActivity() {
    var toolbar: Toolbar? = null
    lateinit var appbar: AppBarLayout
    private var collapsing: CollapsingToolbarLayout? = null
    lateinit var banner: BGABanner
    private lateinit var nested: NestedScrollView
    private lateinit var actors: RecyclerView
    private lateinit var videos: RecyclerView
    private lateinit var description: TextView
    private lateinit var actorAdapter: ActorsAdapter
    private lateinit var videoAdapter: VideosAdapter
    var entity: FilmEntity? = null
    private var state: Int = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_film_detail)
        var ty = intent.extras["entity"]
        entity = Gson().fromJson(ty.toString(), FilmEntity::class.java) as FilmEntity
        initView()
        initData(entity!!)
        initListener()
    }

    private fun initView() {
        appbar = findViewById(R.id.appbar)
        collapsing = findViewById(R.id.collapsing)
        toolbar = findViewById(R.id.toolbar)
        banner = findViewById(R.id.banner)
        nested = findViewById(R.id.nested)
        description = findViewById(R.id.description)
        actors = findViewById(R.id.actors)
        videos = findViewById(R.id.videos)
        var actorsLayoutManager = LinearLayoutManager(this)
        actorsLayoutManager.orientation = LinearLayoutManager.HORIZONTAL
        actors.layoutManager = actorsLayoutManager
        var videosLayoutManager = LinearLayoutManager(this)
        videosLayoutManager.orientation = LinearLayoutManager.HORIZONTAL
        videos.layoutManager = videosLayoutManager
    }

    private fun initData(t: FilmEntity) {
//        toolbar!!.title = entity!!.title
        collapsing!!.title = entity!!.title
        setSupportActionBar(toolbar)
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        description.text = t.description
        actorAdapter = ActorsAdapter(t.actors)
        actors.adapter = actorAdapter
        videoAdapter = VideosAdapter(t.videoUrls)
        videos.adapter = videoAdapter
        initBanner(t.imageUrls)
    }

    private fun initBanner(t: List<String>?) {
        banner.setAdapter { _, itemView, model, _ ->
            XGlide.getInstance().with(this@FilmDetailActivity)
                    .load(model as String)
                    .placeholder(R.drawable.bg_block)
                    .error(R.drawable.bg_block)
                    .into(itemView as ImageView)
                    .displayImage()
        }
        banner.setData(t, null)

        banner.setDelegate { banner, itemView, model, position ->
            ARouter.getInstance()
                    .build("/watercress/video/detail")
                    .navigation()
        }
    }

    private fun initListener() {
        toolbar!!.setNavigationOnClickListener {
            super.onBackPressed()
        }
    }
}

